﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinAPI_GDI_HomeWork1
{
    public partial class Form1 : Form
    {
        private bool check = false;
        private int count = 0;
        private int x1 = 0;
        private int y1 = 0;
        private int x2 = 0;
        private int y2 = 0;
        private int x3 = 0;
        private int y3 = 0;

        public Form1()
        {
            InitializeComponent();
        }

        public void ReloadList()
        {
            Bitmap bm = new Bitmap(ClientSize.Width, ClientSize.Height);

            Graphics gp = Graphics.FromImage(bm);

            Pen p = new Pen(Color.DarkBlue);
            p.Width = 3;

            if (checkBox1.Checked == false && checkBox2.Checked == false && checkBox3.Checked == false && checkBox4.Checked == false)
            {
                gp.DrawLine(p, new Point(x1, y1), new Point(x2, y2));
            }

            if (checkBox1.Checked == true)  // Ellipse
            {
                int centerX = 0, centerY = 0;
                int positionX = 0, positionY = 0;

                centerX = x1 + ((x2 - x1) / 2);
                centerY = y1 + ((y2 - y1) / 2);
                positionX = centerX - ((x2 - x1) / 2);
                positionY = centerY - ((y2 - y1) / 2);

                gp.DrawEllipse(p, new Rectangle(new Point(positionX, positionY), new Size((x2 - x1), (y2 - y1))));
            }

            if (checkBox2.Checked == true)  // Rectangle
            {
                int sizeX = 0, sizeY = 0;
                int changeX = 0, changeY = 0;

                if (x1 > x2 && y1 < y2)
                {
                    changeX = x1;
                    x1 = x2;
                    x2 = changeX;
                }
                if (x1 > x2 && y1 > y2)
                {
                    changeX = x1; changeY = y1;
                    x1 = x2; y1 = y2;
                    x2 = changeX; y2 = changeY;
                }
                if (x1 < x2 && y1 > y2)
                {
                    changeY = y1;
                    y1 = y2;
                    y2 = changeY;
                }

                sizeX = x2 - x1;
                sizeY = y2 - y1;

                gp.DrawRectangle(p, new Rectangle(new Point(x1, y1), new Size(sizeX, sizeY)));
            }

            if (checkBox3.Checked == true)  // Triangle
            {
                Point[] points = { new Point(x1,y1), new Point(x2, y2), new Point(x3, y3)};
                gp.DrawPolygon(p, points);
            }

            if (checkBox4.Checked == true)  // Square
            {
                int sizeX = 0, sizeY = 0;
                int changeX = 0, changeY = 0;

                if (x1 > x2 && y1 < y2)
                {
                    changeX = x1;
                    x1 = x2;
                    x2 = changeX;
                }
                if (x1 > x2 && y1 > y2)
                {
                    changeX = x1; changeY = y1;
                    x1 = x2; y1 = y2;
                    x2 = changeX; y2 = changeY;
                }
                if (x1 < x2 && y1 > y2)
                {
                    changeY = y1;
                    y1 = y2;
                    y2 = changeY;
                }

                sizeX = x2 - x1;

                gp.DrawRectangle(p, new Rectangle(new Point(x1, y1), new Size(sizeX, sizeX)));
            }

            if (checkBox3.Checked == false)
            {
                textBox1.Text = ("x1 = " + x1 + ", y1 = " + y1 +  ";   x2 = " + x2 + ", y2 = " + y2 + ";");
            }

            count = 0;

            gp.Dispose();

            BackgroundImage = bm;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void Form1_Click(object sender, EventArgs e)
        {
            count += 1;
            if (checkBox3.Checked == false)
            {
                if (count == 1)
                {
                    x1 = (e as MouseEventArgs).X;
                    y1 = (e as MouseEventArgs).Y;
                }
                if (count == 2)
                {
                    x2 = (e as MouseEventArgs).X;
                    y2 = (e as MouseEventArgs).Y;

                    ReloadList();
                }
            }
            else
            {
                if (count == 1)
                {
                    x1 = (e as MouseEventArgs).X;
                    y1 = (e as MouseEventArgs).Y;
                }
                if (count == 2)
                {
                    x2 = (e as MouseEventArgs).X;
                    y2 = (e as MouseEventArgs).Y;
                }
                if (count == 3)
                {
                    x3 = (e as MouseEventArgs).X;
                    y3 = (e as MouseEventArgs).Y;

                    ReloadList();
                }
            }
        }

        private void checkBox1_EnabledChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                checkBox2.Checked = false;
                checkBox3.Checked = false;
                checkBox4.Checked = false;
            }
        }

        private void checkBox2_EnabledChanged(object sender, EventArgs e)
        {
            if (checkBox2.Checked == true)
            {
                checkBox1.Checked = false;
                checkBox3.Checked = false;
                checkBox4.Checked = false;
            }
        }

        private void checkBox3_EnabledChanged(object sender, EventArgs e)
        {
            if (checkBox3.Checked == true)
            {
                checkBox1.Checked = false;
                checkBox2.Checked = false;
                checkBox4.Checked = false;
            }
        }

        private void checkBox4_EnabledChanged(object sender, EventArgs e)
        {
            if (checkBox4.Checked == true)
            {
                checkBox1.Checked = false;
                checkBox2.Checked = false;
                checkBox3.Checked = false;
            }
        }
    }
}
